import SwiftUI
import RealityKit
import ARKit

struct ARViewContainer: UIViewRepresentable{
    @EnvironmentObject var arViewModel :ARViewModel
    @Binding var addCube : Bool
    
    func makeUIView(context: Context) -> ARView {
        return arViewModel.arView
    }
    func updateUIView(_ uiView: ARView, context: Context) {
        if addCube{
            let boxMesh = MeshResource.generateBox(size: 0.1)
            let material = SimpleMaterial(color:.blue,isMetallic: false)
            let modelEntity = ModelEntity(mesh: boxMesh,materials: [material])
            let anchorEntity = AnchorEntity(plane: .any)
            
            anchorEntity.addChild(modelEntity)
            arViewModel.arView.scene.addAnchor(anchorEntity)
            DispatchQueue.main.async {
                addCube = false
            }
        }
    }
}
